﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Data.Common;

namespace CMS.Web.CompDenu
{
    public partial class AddDonThu : BaseUserControl, IAddDonThuView
    {
        private AddDonThuPresenter presenter;
        // For Nguoi su dung
        IList<LoaiDon> ldList;
        IList<LinhVuc> lvList;
        IList<NguonTiepDon> ntdList;
        IList<CachXL> xlList;
        IList<DiaBan> dbList;
        IList<PhongBan> pbList;
        IList<TrangThaiXuLy> ttxlList;
        IList<NguoiSuDung> nsdList;
        NguoiSuDungInfo userInfo = null;
        String donthuID = "";
        DonThu _donThu = null;
        NguoiGuiDon _nguoiGuiDon = null;
        ChuyenTiep _chuyenTiep;
        // statDone: -1 - Chưa thực hiện; 1 - Thành công; 0 - Lỗi
        public int statDone = -1;

        public AddDonThu()
        {
            this.presenter = new AddDonThuPresenter(this, NHibernateDaoFactory.Instance.GetDonThuDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            donthuID = Request.QueryString.Get("objID")==null?"" : Request.QueryString.Get("objID");
            statDone = Request.QueryString.Get("statDone") == null ? -1 : Convert.ToInt32(Request.QueryString.Get("statDone"));
            
            this.RaiseClientEvent();
            if (!donthuID.Equals(""))
            {
                this.lblMess.InnerHtml = "";
                _donThu = DonThuDao.Instance.GetById(donthuID);
                if (_donThu.ChuyenTieps.Count > 0)
                {
                    _chuyenTiep = _donThu.ChuyenTieps[0];
                }
                else
                {
                    _chuyenTiep = new ChuyenTiep();
                    _chuyenTiep.MaDonThu = _donThu.ID;
                }
                // Get nguoi gui don
                _nguoiGuiDon = _donThu.NguoiGuiLookup;
            }
            if (!this.IsPostBack)
            {
                if (donthuID.Equals(""))
                {
                    this.InitForm(); 
                }
                else
                {
                    this.lblTitle.InnerHtml = "HIỆU CHỈNH ĐƠN THƯ";
                    this.presenter.LoadViewFromModel();
                    InitFormEdit(_donThu);
                }
                if (statDone != -1)
                {
                    rowMess.Visible = true;
                    this.lblMess.Visible = true;
                    if (statDone == 1)
                    {
                        this.lblMess.InnerHtml = "Cập nhật thành công!";
                    }
                    else if (statDone == 0)
                    {
                        this.lblMess.InnerHtml = "Lỗi cập nhật dữ liệu!";
                    }
                }
            }
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            rowMess.Visible = false;
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.AddDonThuPresenter));
        }

        public string Message
        {
            set { //lblMessage.Text = value; 
            }
        }

        public DonThu DonThuToEdit
        {
            get
            {
                return _donThu;
            }
            set
            {
                this._donThu = value;
            }
        }

        public DonThu DonThuToAdd
        {
            get
            {   
                DonThu dtObj;

                if (DonThuToEdit != null)
                {
                    dtObj = DonThuToEdit;
                }
                else
                {
                    dtObj = new DonThu();
                    if (hidNguoiGui.Value != "0")
                        dtObj.MaNguoiGui = Convert.ToInt32(hidNguoiGui.Value);
                }
                string[] temp = null;
                if (!txtSoThuLy.Text.Equals(""))
                    dtObj.SoThuLy = txtSoThuLy.Text.Trim();// Convert.ToInt32(txtSoThuLy.Text.Trim());
                else
                    dtObj.SoThuLy = null;
                try
                {
                    if (txtDonDeNgay.Text.Trim().Equals(""))
                    {
                        dtObj.DonDeNgay = null;
                    }
                    else
                    {
                        temp = txtDonDeNgay.Text.Trim().Split(new Char[] { '/' });
                        dtObj.DonDeNgay = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                //try
                //{
                //    if (txtDonDeNgay.Text.Trim().Equals(""))
                //    {
                //        dtObj.DonDeNgay = null;
                //    }
                //    else
                //    {
                //        temp = txtNgayGui.Text.Trim().Split(new Char[] { '/' });
                //        dtObj.NgayGui = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                //    }
                //}
                //catch { }
                try
                {
                    
                        temp = txtNgayGui.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayGui = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    
                }
                catch { }
                try
                {
                    if (txtNgayGiaiQuyet.Text.Trim().Equals(""))
                    {
                        dtObj.NgayGiaiQuyet = null;
                    }
                    else
                    {
                        temp = txtNgayGiaiQuyet.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayGiaiQuyet = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayBaoCao.Text.Trim().Equals(""))
                    {
                        dtObj.NgayBaoCao = null;
                    }
                    else
                    {
                        temp = txtNgayBaoCao.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayBaoCao = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayThuLy.Text.Trim().Equals(""))
                    {
                        dtObj.NgayThuLy = null;
                    }
                    else
                    {
                        temp = txtNgayThuLy.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayThuLy = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayHoanTra.Text.Trim().Equals(""))
                    {
                        dtObj.NgayHoanTra = null;
                    }
                    else
                    {
                        temp = txtNgayHoanTra.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayHoanTra = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayChuyenKN.Text.Trim().Equals(""))
                    {
                        dtObj.NgayChuyenKN = null;
                    }
                    else
                    {
                        temp = txtNgayChuyenKN.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayChuyenKN = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayThongBao.Text.Trim().Equals(""))
                    {
                        dtObj.NgayThongBao = null;
                    }
                    else
                    {
                        temp = txtNgayThongBao.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayThongBao = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayNhanKet.Text.Trim().Equals(""))
                    {
                        dtObj.NgayNhanKQ = null;
                    }
                    else
                    {
                        temp = txtNgayNhanKet.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayNhanKQ = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }
                try
                {
                    if (txtNgayHoanThanh.Text.Trim().Equals(""))
                    {
                        dtObj.NgayHoanThanh = null;
                    }
                    else
                    {
                        temp = txtNgayHoanThanh.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayHoanThanh = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { }

                dtObj.GhiChu = txtGhiChuXL.Text;
                dtObj.VBGNV = txtVBGNV.Text;
                dtObj.KhongGioiHan = chkNgayHoanThanh.Checked;
                dtObj.LanGui = Convert.ToByte(txtLanGui.Text.Trim());
                dtObj.MaDiaBan = Convert.ToInt32(hidDiaBan.Value);
                dtObj.MaLoaiDon = Convert.ToInt32(drpLoaiDon.SelectedValue);
                dtObj.MaLinhVuc = Convert.ToInt32(drpLinhVuc.SelectedValue);
                dtObj.DongGui = txtDongGui.Text.Trim();
                dtObj.NoiDung = txtTomTat.Text.Trim();
                dtObj.DeXuatYKien = txtDeXuatYKien.Text.Trim();
                dtObj.KetQuaGiaiQuyet = txtKetQuaGiaiQuyet.Text.Trim();
                dtObj.NgayCapNhat = DateTime.Now;
                dtObj.NgayKetThucThucTe = DateTime.Now;
                dtObj.MaTTXL = Convert.ToInt32(drpTTXL.SelectedValue);
                dtObj.LoaiHinh = Convert.ToInt32(drpLoaiHinh.SelectedValue);
                dtObj.NguonTiepDon = Convert.ToInt32(drpNguonTiepDon.SelectedValue);
                if (drpCachXL.SelectedValue.Equals("0"))
                    dtObj.MaCachXL = null;
                else
                    dtObj.MaCachXL = Convert.ToInt32(drpCachXL.SelectedValue);
                if (drpPhongBan.SelectedValue.Equals("0"))
                    dtObj.MaPhongBan = null;
                else
                    dtObj.MaPhongBan = drpPhongBan.SelectedValue;
                dtObj.NguoiNhanDon = txtNguoiNhanDon.Text.Trim();
                dtObj.NoiDungHuongDan = txtNoiDungHuongDan.Text.Trim();
                dtObj.NoiChuyen = txtNoiChuyen.Text.Trim();
                dtObj.NoiDen = txtNoiDen.Text.Trim();
                //dtObj.DonViGiaiQuyet = txtDonViGiaiQuyet.Text.Trim();
                dtObj.DSTapTheDeDon = txtDSTapTheDeDon.Text.Trim();
                dtObj.NoiDungPhieuTra = txtNoiDungPhieuTra.Text.Trim();

                dtObj.LyDoTra = txtLyDoTra.Text.Trim();                
                if (radStatus.SelectedValue.Equals("true"))
                {
                    dtObj.TrangThai = 1;
                }
                else
                {
                    dtObj.TrangThai = 0;
                }
                dtObj.NguoiSoanThao = hidNguoiChuyen.Value;
                if (!this.VanbanCD.Equals("") && this.VanbanCD!=null){
                    dtObj.VBChiDao = true;
                }else{
                    dtObj.VBChiDao = false;
                }

                //if (!this.PhongBanIds.Equals("") && this.PhongBanIds != null)
                //{
                //    dtObj.PBXuLy = true;
                //}
                //else
                //{
                //    dtObj.PBXuLy = false;
                //}

                if (donthuID.Equals(""))
                {
                    //File dinh kem
                    IList<FileDinhKem> FileList = this.UploadFile();
                    FileList = this.presenter.SaveFileDinhKem(FileList);
                    dtObj.DinhKem = (FileList.Count > 0);
                    dtObj.FileDinhKems = FileList;
                    foreach (FileDinhKem fdk in FileList)
                    {
                        fdk.DonThu = dtObj;
                    }
                }
                else
                {
                    IList<FileDinhKem> FileList = dtObj.FileDinhKems;
                    if (FileList.Count > 0)
                    {
                        for (int i = FileList.Count - 1; i >= 0; i--)
                        {
                            if (hidFileDinhKem.Value.IndexOf(FileList[i].ID) < 0)
                            {
                                FileList.RemoveAt(i);
                            }
                        }
                    }

                    IList<FileDinhKem> NewFileList = this.UploadFile();
                    NewFileList = this.presenter.SaveFileDinhKem(NewFileList);
                    foreach (FileDinhKem fdk in NewFileList)
                    {
                        fdk.DonThu = dtObj;
                        FileList.Add(fdk);
                    }
                    dtObj.DinhKem = (FileList.Count > 0);
                    dtObj.FileDinhKems = FileList;
                }


                return dtObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }

        public NguoiGuiDon NguoiGuiToEdit
        {
            get
            {
                return _nguoiGuiDon;
            }
            set
            {
                this._nguoiGuiDon = value;
            }
        }

        public NguoiGuiDon NguoiGuiToAdd
        {
            get
            {
                NguoiGuiDon dtObj = null;

                if (NguoiGuiToEdit != null)
                {
                    dtObj = NguoiGuiToEdit;
                }
                else
                {
                    dtObj = new NguoiGuiDon();
                }
                if (hidNguoiGui.Value.Equals("0"))
                {
                    dtObj.HoTen = txtHoTen.Text.Trim();
                    dtObj.DiaChi = txtDiaChi.Text.Trim();
                    //dtObj.MaDonVi = Int32.Parse(hidDonVi.Value);
                    //dtObj.TenDonVi = drpDonVi.Text.Trim();
                }
                return dtObj;
            }
        }

        
        public ChuyenTiep ChuyenTiepToAdd
        {
            get
            {
                ChuyenTiep ctObj = ChuyenTiepToEdit;
                if (ctObj == null)
                    ctObj = new ChuyenTiep();
                ctObj.MaNguoiChuyen = hidNguoiChuyen.Value;
                ctObj.MaNguoiNhan = hidNguoiXuLy.Value;
                ctObj.YKienLDVP = txtYKienLDVP.Text.Trim();
                ctObj.YKienLDUBND = txtYKienLDUBND.Text.Trim();
                ctObj.NgayChuyen = DateTime.Now;
                return ctObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }

        public ChuyenTiep ChuyenTiepToEdit
        {
            get
            {
                return _chuyenTiep;
            }
            set
            {
                this._chuyenTiep = value;
            }
        }

        // For Dia ban
        public IList<DiaBan> DiaBanList
        {
            get { return this.dbList; }
            set { this.dbList = value; }
        }
        // For Loai don
        public IList<LoaiDon> LoaiDonList
        {
            get { return this.ldList; }
            set { this.ldList = value; }
        }
        // For Linh vuc
        public IList<LinhVuc> LinhVucList
        {
            get { return this.lvList; }
            set { this.lvList = value; }
        }
        // For NguonTiepDon
        public IList<NguonTiepDon> NguonTiepDonList
        {
            get { return this.ntdList; }
            set { this.ntdList = value; }
        }
        public IList<CachXL> CachXLList
        {
            get { return this.xlList; }
            set { this.xlList = value; }
        }
        // For Phong ban
        public IList<PhongBan> PhongBanList
        {
            get { return this.pbList; }
            set { this.pbList = value; }
        }
        // For Trang thai xu ly
        public IList<TrangThaiXuLy> TTXLList
        {
            get { return this.ttxlList; }
            set { this.ttxlList = value; }
        }
        // For Nguoi su dung
        public IList<NguoiSuDung> NguoiSuDungList
        {
            get { return this.nsdList; }
            set { this.nsdList = value; }
        }
        // For Nguoi su dung
        public string getTenNguoiSuDung(string code)
        {
            return presenter.getNguoiSuDung(code).TenNguoiSuDung;
        }

        public String VanbanCD
        {
            get { return this.hidVBCD.Value; }
            set { this.hidVBCD.Value = value; }
        }

        public IList<VanBan> VBCDs
        {
            set
            {
                this.drpVBCD.DataSource = value;
                this.drpVBCD.DataBind();
            }
        }

        public String ID
        {
            get { return this.donthuID; }
            set { this.ID = value; }
        }

        public int LoaiPhieu
        {
            get { return 0;// drpLoaiPhieu.SelectedIndex; 
            }
        }
        /// <summary>
        /// Save button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            statDone = (presenter.AddObj(Page.IsValid)) ? 1 : 0;

            if (presenter.GetDonThuID().Equals(""))
            {
                Response.Redirect(BaseUrl + "&action=EDIT&statDone=" + Convert.ToString(statDone) + "&objID=" + ID);
            }
            else
            {
                Response.Redirect(BaseUrl + "&action=EDIT&statDone=" + Convert.ToString(statDone) + "&objID=" + presenter.GetDonThuID());
            }
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            presenter.AddObj(Page.IsValid);
            Response.Redirect(BaseUrl + "&action=EDIT&objID=" + ID);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW");
        }

        protected void InitForm()
        {
            txtSoThuLy.Text = "";// Convert.ToString(presenter.GetMaxSoThuLy());
            //txtDonDeNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtNgayGui.Text = DateTime.Now.ToString("dd/MM/yyyy");
            //txtDonDeNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            //txtNgayBaoCao.Text = DateTime.Now.ToString("dd/MM/yyyy");
            //txtNgayGiaiQuyet.Text = DateTime.Now.ToString("dd/MM/yyyy");
            //txtNgayThuLy.Text = DateTime.Now.ToString("dd/MM/yyyy");
            //txtNgayHoanTra.Text = DateTime.Now.ToString("dd/MM/yyyy");
            //txtNgayChuyenKN.Text = DateTime.Now.ToString("dd/MM/yyyy");
            //txtNgayThongBao.Text = DateTime.Now.ToString("dd/MM/yyyy");

            txtHoTen.Text = "";
            txtLanGui.Text = "1";
            txtDiaChi.Text = "";
            txtDongGui.Text = "";
            txtTomTat.Text = "";
            txtDeXuatYKien.Text = "";
            txtYKienLDVP.Text = "";
            txtYKienLDUBND.Text = "";
            txtKetQuaGiaiQuyet.Text = "";
            //txtNguonTiepDon.Text = "";
            txtNguoiNhanDon.Text = "";
            txtNoiDungHuongDan.Text = "";
            //txtDonViGiaiQuyet.Text = "";
            txtNoiChuyen.Text = "";
            txtNoiDen.Text = "";
            txtDSTapTheDeDon.Text = "";
            txtNoiDungPhieuTra.Text = "";
            txtLyDoTra.Text = "";

            txtGhiChuXL.Text = "";
            txtVBGNV.Text = "";
            chkNgayHoanThanh.Checked = false;
            
            //lblNguoiVaoSo.Text = ((CMS.DOMS.Web.Site)this.Page.Master).userInfo.TenNguoiSuDung;
            hidNguoiXuLy.Value = (userInfo == null) ? "" : userInfo.ID;
            hidNguoiChuyen.Value = (userInfo==null)?"":userInfo.ID;
            hidNguoiNhan.Value = (userInfo == null) ? "" : userInfo.ID;
            // For Dia ban
            this.drpDiaBan.ReadOnly = true;
            // For Loai don
            this.drpLoaiDon.DataSource = this.LoaiDonList;
            drpLoaiDon.DataTextField = "TenLoaiDon";
            drpLoaiDon.DataValueField = "ID";
            this.drpLoaiDon.DataBind();
            // For Linh vuc
            this.drpLinhVuc.DataSource = this.LinhVucList;
            drpLinhVuc.DataTextField = "TenLinhVuc";
            drpLinhVuc.DataValueField = "ID";
            this.drpLinhVuc.DataBind();
            // For NguonTiepDon
            this.drpNguonTiepDon.DataSource = this.NguonTiepDonList;
            drpNguonTiepDon.DataTextField = "TenNguonTiep";
            drpNguonTiepDon.DataValueField = "ID";
            this.drpNguonTiepDon.DataBind();

            // For CachXL
            this.drpCachXL.DataSource = this.CachXLList;
            drpCachXL.DataTextField = "TenCachXL";
            drpCachXL.DataValueField = "ID";
            this.drpCachXL.DataBind();
            this.drpCachXL.Items.Insert(0, new ListItem("", "0"));

            // For Linh vuc
            this.drpPhongBan.DataSource = this.PhongBanList;
            drpPhongBan.DataTextField = "TenPhongBan";
            drpPhongBan.DataValueField = "ID";
            this.drpPhongBan.DataBind();
            this.drpPhongBan.Items.Insert(0, new ListItem("", "0"));

            // For TTXL
            this.drpTTXL.DataSource = this.TTXLList;
            drpTTXL.DataTextField = "TenTTXL";
            drpTTXL.DataValueField = "ID";
            this.drpTTXL.DataBind();
            //this.drpNguonTiepDon.ClearSelection();
            //this.drpNguonTiepDon.SelectedIndex = 0;
        }
        
        protected void InitFormTest()
        {
            txtSoThuLy.Text = "12";
            txtDonDeNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtNgayGui.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtHoTen.Text = "Nguyễn Văn Linh";
            txtLanGui.Text = "1";
            txtDiaChi.Text = "Đống đa - Hà Nội";
            txtDongGui.Text = "member@huesoft.com.vn";
            txtTomTat.Text = "Khiếu nại vì chưa được khiếu nại";
            txtDeXuatYKien.Text = "Thích thì xử lý";
            txtYKienLDVP.Text = "Xử lý";
            txtYKienLDUBND.Text = "Xử lý lại";
            txtKetQuaGiaiQuyet.Text = "OK";
            //lblNguoiVaoSo.Text = ((CMS.DOMS.Web.Site)this.Page.Master).userInfo.TenNguoiSuDung;
            hidNguoiChuyen.Value = (userInfo == null) ? "" : userInfo.ID;
            hidNguoiNhan.Value = (userInfo == null) ? "" : userInfo.ID;
            // For Loai don
            this.drpLoaiDon.DataSource = this.LoaiDonList;
            drpLoaiDon.DataTextField = "TenLoaiDon";
            drpLoaiDon.DataValueField = "ID";
            this.drpLoaiDon.DataBind();
            // For Linh vuc
            this.drpLinhVuc.DataSource = this.LinhVucList;
            drpLinhVuc.DataTextField = "TenLinhVuc";
            drpLinhVuc.DataValueField = "ID";
            this.drpLinhVuc.DataBind();
        }
        
        protected void InitFormEdit(DonThu obj)
        {
            NguoiGuiDon objTemp = obj.NguoiGuiLookup;
            txtHoTen.Text = objTemp.HoTen;
            txtDiaChi.Text = objTemp.DiaChi;

            txtSoThuLy.Text = (obj.SoThuLy != null) ? obj.SoThuLy : "";
            txtDonDeNgay.Text = (obj.DonDeNgay != null) ? ((DateTime)obj.DonDeNgay).ToString("dd/MM/yyyy") : "";
            txtNgayGui.Text = obj.NgayGui.ToString("dd/MM/yyyy");
            txtNgayGiaiQuyet.Text = (obj.NgayGiaiQuyet != null) ? ((DateTime)obj.NgayGiaiQuyet).ToString("dd/MM/yyyy") : "";
            txtNgayBaoCao.Text = (obj.NgayBaoCao != null) ? ((DateTime)obj.NgayBaoCao).ToString("dd/MM/yyyy") : "";
            txtNgayThuLy.Text = (obj.NgayThuLy != null) ? ((DateTime)obj.NgayThuLy).ToString("dd/MM/yyyy") : "";
            txtNgayHoanTra.Text = (obj.NgayHoanTra != null) ? ((DateTime)obj.NgayHoanTra).ToString("dd/MM/yyyy") : "";
            txtNgayChuyenKN.Text = (obj.NgayChuyenKN != null) ? ((DateTime)obj.NgayChuyenKN).ToString("dd/MM/yyyy") : "";
            txtNgayThongBao.Text = (obj.NgayThongBao != null) ? ((DateTime)obj.NgayThongBao).ToString("dd/MM/yyyy") : "";

            txtNgayNhanKet.Text = (obj.NgayNhanKQ != null) ? ((DateTime)obj.NgayNhanKQ).ToString("dd/MM/yyyy") : "";
            txtNgayHoanThanh.Text = (obj.NgayHoanThanh != null) ? ((DateTime)obj.NgayHoanThanh).ToString("dd/MM/yyyy") : "";
            txtGhiChuXL.Text = obj.GhiChu;
            txtVBGNV.Text = obj.VBGNV;
            chkNgayHoanThanh.Checked = obj.KhongGioiHan;

            txtLanGui.Text = obj.LanGui.ToString();
            // For Dia ban
            this.drpDiaBan.Text = obj.DiaBan.TenDiaBan;
            this.drpDiaBan.ReadOnly = true;
            this.hidDiaBan.Value = Convert.ToString(obj.MaDiaBan);
            // For Loai don
            this.drpLoaiDon.DataSource = this.LoaiDonList;
            drpLoaiDon.DataTextField = "TenLoaiDon";
            drpLoaiDon.DataValueField = "ID";
            this.drpLoaiDon.DataBind();
            ListItem item = drpLoaiDon.Items.FindByValue(obj.MaLoaiDon.ToString());
            if (item != null) item.Selected = true;
            // For Linh vuc
            this.drpLinhVuc.DataSource = this.LinhVucList;
            drpLinhVuc.DataTextField = "TenLinhVuc";
            drpLinhVuc.DataValueField = "ID";
            this.drpLinhVuc.DataBind();
            item = drpLinhVuc.Items.FindByValue(obj.MaLinhVuc.ToString());
            if (item != null) item.Selected = true;
            // For NguonTiepDon
            this.drpNguonTiepDon.DataSource = this.NguonTiepDonList;
            drpNguonTiepDon.DataTextField = "TenNguonTiep";
            drpNguonTiepDon.DataValueField = "ID";
            this.drpNguonTiepDon.DataBind();
            item = drpNguonTiepDon.Items.FindByValue(obj.MaNguonTiepDon.ToString());
            if (item != null) item.Selected = true;

            // For NguonTiepDon
            this.drpCachXL.DataSource = this.CachXLList;
            drpCachXL.DataTextField = "TenCachXL";
            drpCachXL.DataValueField = "ID";
            this.drpCachXL.DataBind();
            this.drpCachXL.Items.Insert(0, new ListItem("", "0"));
            item = drpCachXL.Items.FindByValue(obj.MaCachXL.ToString());
            if (item != null) item.Selected = true;

            // For Phong ban
            this.drpPhongBan.DataSource = this.PhongBanList;
            drpPhongBan.DataTextField = "TenPhongBan";
            drpPhongBan.DataValueField = "ID";
            this.drpPhongBan.DataBind();
            this.drpPhongBan.Items.Insert(0, new ListItem("", "0"));
            item = drpPhongBan.Items.FindByValue(obj.MaPhongBan);
            if (item != null) item.Selected = true;

            txtDongGui.Text = obj.DongGui;
            txtTomTat.Text = obj.NoiDung;
            txtDeXuatYKien.Text = obj.DeXuatYKien;
            //txtNguonTiepDon.Text = obj.NguonTiepDon;
            txtNguoiNhanDon.Text = obj.NguoiNhanDon;
            txtNoiDungHuongDan.Text = obj.NoiDungHuongDan;
            txtNoiChuyen.Text = obj.NoiChuyen;
            txtNoiDen.Text = obj.NoiDen;
            //txtDonViGiaiQuyet.Text = obj.DonViGiaiQuyet;
            txtDSTapTheDeDon.Text = obj.DSTapTheDeDon;
            txtNoiDungPhieuTra.Text = obj.NoiDungPhieuTra;
            txtLyDoTra.Text = obj.LyDoTra;
            //set loai hinh
            item = drpLoaiHinh.Items.FindByValue(obj.LoaiHinh.ToString());
            if (item != null) item.Selected = true;

            foreach (FileDinhKem fdk in obj.FileDinhKems)
            {
                hidFileDinhKem.Value += (hidFileDinhKem.Value == "") ? fdk.ID : "," + fdk.ID;
            }
            if (obj.ChuyenTieps.Count>0){
                txtYKienLDVP.Text = obj.ChuyenTieps[0].YKienLDVP == null ? "": obj.ChuyenTieps[0].YKienLDVP;
                txtYKienLDUBND.Text = obj.ChuyenTieps[0].YKienLDUBND==null ? "":obj.ChuyenTieps[0].YKienLDUBND;
                hidNguoiNhan.Value = obj.ChuyenTieps[0].MaNguoiNhan;
                hidNguoiChuyen.Value = obj.ChuyenTieps[0].MaNguoiChuyen;
            }
            txtKetQuaGiaiQuyet.Text = obj.KetQuaGiaiQuyet;
            hidNguoiChuyen.Value = obj.NguoiSoanThao;
            // For TTXL
            this.drpTTXL.DataSource = this.TTXLList;
            drpTTXL.DataTextField = "TenTTXL";
            drpTTXL.DataValueField = "ID";
            this.drpTTXL.DataBind();
            item = drpTTXL.Items.FindByValue(obj.MaTTXL.ToString());
            if (item != null) item.Selected = true;
            if (obj.TrangThai == 1)
            {
                radStatus.SelectedIndex = 0;
            }
            else
            {
                radStatus.SelectedIndex = 1;
            }
            item = drpNguonTiepDon.Items.FindByValue(obj.NguonTiepDon.ToString());
            if (item != null) item.Selected = true;
        }

        /// <summary>
        /// UploadFile
        /// </summary>
        /// <returns></returns>
        protected IList<FileDinhKem> UploadFile()
        {
            IList<FileDinhKem> FileList = new List<FileDinhKem>();
            FileDinhKem FDK = null;
            if (this.Request.Files.Count > 0)
            {
                // get the applications path
                string UploadPath = DownloadAssistant.UploadPath;
                // loop through all the uploaded files
                for (int j = 0; j < this.Request.Files.Count; j++)
                {
                    // get the current file
                    HttpPostedFile UploadFile = this.Request.Files[j];
                    // if there was a file uploded
                    if (UploadFile != null && UploadFile.ContentLength > 0)
                    {
                        string strEx = System.IO.Path.GetExtension(UploadFile.FileName);
                        string FileName = System.IO.Path.GetFileName(UploadFile.FileName).Replace("&", "").Replace("=", "");
                        int iEx = 0;
                        while (System.IO.File.Exists(UploadPath + FileName))
                        {
                            iEx++;
                            FileName = System.IO.Path.GetFileNameWithoutExtension(UploadFile.FileName).Replace("&", "").Replace("=", "") + "_" + iEx.ToString() + strEx;
                        }
                        UploadFile.SaveAs(UploadPath + FileName);

                        FDK = new FileDinhKem();
                        FDK.TenFileDinhKem = FileName;
                        FileList.Add(FDK);
                    }
                }
            }
            return FileList;
        }

        #region IAddDonThuView Members


        //public string PhongBanIds
        //{
        //    get { return this.hidPhongBan.Value; }
        //    set { this.hidPhongBan.Value = value; }
        //}

        //public IList<PhongBan> PhongBanXLs
        //{
        //    set
        //    {
        //        this.lstPhongBan.DataSource = value;
        //        this.lstPhongBan.DataBind();
        //    }
        //}

        #endregion
    }
}